﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WEBWeixinSDK;

namespace WxLogin
{
    public partial class MainL : UserControl
    {
        public MainL()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            SysLog.GetInstance().NewLogWrite += MainForm_NewLogWrite;
        }
        string UinName = Weixin.Uin;
        string Namestr = Weixin.Namestr;
        private void MainForm_NewLogWrite(object sender, CQLogEventArgs e)
        {
            txtlog.Text = e.LogMessage.ToString() + "\r\n" + txtlog.Text;
          
            // txtlog.AppendText; ;
        }
        private void MainL_Load(object sender, EventArgs e)
        {
           
            REUdate();
          // Weixin. Proc_msg(UinName);
        }
        private void UpdateFriends()
        {
            // listfriends.Clear();
            listfriends.Items.Clear();
            if (!string.IsNullOrEmpty (UinName))
            {
                foreach (KeyValuePair<string, WXInit.MemUser> item in Weixin.UinUser[UinName])
                {
                    listfriends.Items.Add(new ListViewItem(new string[] {
                    item.Key.ToString(),
                    item.Value.NickName.ToString (),
                     item.Value.Signature .ToString (),
                  item.Value.City .ToString (),
                    item.Value.Sex.ToString(),
                       item.Value.RemarkName .ToString()

              }));

                }

            }

        }
        private void UpdateGroup()
        {
            // listgorug.Clear();
            listgorug.Items.Clear();
            if (!string.IsNullOrEmpty(UinName))
            {
                foreach (var item in Weixin.UinChat[UinName])
                {
                    listgorug.Items.Add(new ListViewItem(new string[] {
                    item.Key.ToString(),
                    item.Value.NickName,
                     item.Value.Signature ,

                       item.Value.City .ToString()

              }));
                }
            }
        }
        private void UpdateChat()
        {
            listchat.Items.Clear();
            if (!string.IsNullOrEmpty(UinName))
            {
                foreach (var item in Weixin.UinGroup[UinName])
                {
                    listchat.Items.Add(new ListViewItem(new string[] {
                    item.Key.ToString(),
                    item.Value.NickName,
                     item.Value.Signature ,
                       item.Value.City   .ToString()
              }));
                }
            }
        }
        private void UpdateOther()
        {
            lvother.Items.Clear();
            if (!string.IsNullOrEmpty(UinName))
            {
                if (Weixin.OtherMe != null)
            {
                    foreach (var item in Weixin.UinOtherMe[UinName])
                    {
                        lvother.Items.Add(new ListViewItem(new string[] {
                    item.Key.ToString(),
                    item.Value.NickName,
                     item.Value.Signature ,
                       item.Value.City  .ToString()

              }));
                    }
                }
            }

        }
        private void UpdateOwn()
        {
            if ( !string.IsNullOrEmpty ( UinName)  )
            {
                foreach (var item in Weixin.UinTUser[UinName])
                {
                    toolStripStatusLabel1.Text = "登录用户：" + item.Value.NickName.ToString();
                    toolStripStatusLabel2.Text = "Uin ：" + item.Value.Uin.ToString();
                    Namestr = item.Key.ToString();
                    UinName = item.Value.Uin.ToString();
                }
            }
           
        }
        public void REUdate()
        {
           
                UpdateOwn();
                UpdateFriends();
                UpdateGroup();
                UpdateChat();
                UpdateOther();
          
        }
        string qunhao = "";
        string qunuin = "";
        int Mennum = 0;


        private void Listgorug_Click(object sender, EventArgs e)
        {

            if (listgorug.Created == true)
            {
                for (int i = 0; i < listgorug.SelectedItems.Count; i++)
                {
                    var item = listgorug.SelectedItems[i];
                    qunuin = item.SubItems[0].Text.Trim();
                    qunhao = item.SubItems[1].Text.Trim();
                }
                lvgourgfriend.Items.Clear();
                foreach (var fr in Weixin.GroupUser)
                {
                    if (qunuin == fr.Key)
                    {
                        foreach (var fra in fr.Value.MemberList)
                        {
                            lvgourgfriend.Items.Add(new ListViewItem(new string[] {
                   fra.UserName,
                                fra.NickName.ToString (),
                         fr.Value.NickName,
         fr.Value.EncryChatRoomId.ToString (),
                                fr.Value.MemberCount.ToString ()

             }));
                        }
                    }
                }
            }
            label2.Text = "在群 " + qunhao + " 聊天中";
            Mennum = 0;
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            if (Mennum == 0 && !string.IsNullOrEmpty(textBox4.Text))
            {
                Weixin.SendMsg(textBox4.Text.Trim(), Namestr, qunuin, 1, UinName);
            }
            if (Mennum == 1 && !string.IsNullOrEmpty(textBox4.Text))
            {
                Weixin.SendMsg(textBox4.Text.Trim(), Namestr, qunuin, 1, UinName);
            }
        }

        private void Lvgourgfriend_Click(object sender, EventArgs e)
        {
            if (lvgourgfriend.Created == true)
            {
                for (int i = 0; i < lvgourgfriend.SelectedItems.Count; i++)
                {
                    var item = lvgourgfriend.SelectedItems[i];
                    qunuin = item.SubItems[0].Text.Trim();
                    qunhao = item.SubItems[1].Text.Trim();
                }
            }
            Mennum = 1;
            label2.Text = "与群员 " + qunhao + " 私聊中";
        }

        private void Listfriends_Click(object sender, EventArgs e)
        {
            if (listfriends.Created == true)
            {
                for (int i = 0; i < listfriends.SelectedItems.Count; i++)
                {
                    var item = listfriends.SelectedItems[i];
                    qunuin = item.SubItems[0].Text.Trim();
                    qunhao = item.SubItems[1].Text.Trim();
                }
            }
            Mennum = 2;
            label1.Text = "与好友 " + qunhao + " 聊天中";
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (Mennum == 2 && !string.IsNullOrEmpty(textBox3.Text))
            {
                Weixin.SendMsg(textBox3.Text.Trim(), Namestr, qunuin.Trim(), 1, UinName);
            }
        }

        private void OtherClick(object sender, EventArgs e)
        {
            if (listchat.Created == true)
            {
                for (int i = 0; i < listchat.SelectedItems.Count; i++)
                {
                    var item = listchat.SelectedItems[i];
                    qunuin = item.SubItems[0].Text.Trim();
                    qunhao = item.SubItems[1].Text.Trim();
                }
            }
            Mennum = 3;
            label3.Text = "在公众号与 " + qunhao + " 聊天中";
        }

        private void Button4_Click_1(object sender, EventArgs e)
        {
            if (Mennum == 4 && !string.IsNullOrEmpty(textBox8.Text))
            {
                Weixin.SendMsg(textBox8.Text.Trim(), Namestr, qunuin, 1, UinName);
            }
        }

        private void Button3_Click(object sender, EventArgs e)
        {

            if (Mennum == 3 && !string.IsNullOrEmpty(textBox6.Text))
            {
                Weixin.SendMsg(textBox6.Text.Trim(), Namestr, qunuin, 1, UinName);
            }
        }

        private void Lvother_Click(object sender, EventArgs e)
        {
            if (lvother.Created == true)
            {
                for (int i = 0; i < lvother.SelectedItems.Count; i++)
                {
                    var item = lvother.SelectedItems[i];
                    qunuin = item.SubItems[0].Text.Trim();
                    qunhao = item.SubItems[1].Text.Trim();
                }
            }
            Mennum = 4;
            label3.Text = "在其他应用与 " + qunhao + " 聊天中";
        }
    }
}
